package com.zit.java.service.login;

import com.zit.java.dao.LoginMapper;
import com.zit.java.dao.SysMenuMapper;
import com.zit.java.pojo.Account;
import com.zit.java.pojo.MenuInfo;
import com.zit.java.pojo.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper dao;

    @Autowired
    private SysMenuMapper sysMenuDao;

    @Override
    public Account findByAccName(String name) {
        return dao.findByAccName(name);
    }

    @Override
    public int updatePassword(String newPassword, String id) {
        int rst = dao.updatePassword(newPassword, id);
        return rst;
    }

    @Override
    public Map<String, Object> queryMenu1(Integer userId) {
        Map<String, Object> mapMenuComp = new HashMap<>();
        //首页信息
        Map<String, String> homeInfoMap = new HashMap<>();
        homeInfoMap.put("title", "首页");
        homeInfoMap.put("href", "login/homePage");
        //放map进行返回数据
        mapMenuComp.put("homeInfo", homeInfoMap);

        //logo图标
        Map<String, String> logoInfo = new HashMap<>();
        logoInfo.put("title", "郑州科技学院");
        logoInfo.put("image", "static/images/logo.png");
        logoInfo.put("href", "http://www.zit.edu.cn");
        mapMenuComp.put("logoInfo", logoInfo);

//        查询父级菜单信息,当前用户的菜单
        List<MenuInfo> menuInfo = this.queryMenuInfo(userId, 0);
      //查询所有的菜单消息
        // List<MenuInfo> menuInfo = this.queryMenuInfo(0);
        mapMenuComp.put("menuInfo", menuInfo);
        return mapMenuComp;
    }

    @Override
    public int insertUser(Account account) {
        int rst = dao.insertSelective(account);
        return rst;
    }

    @Override
    public List<String> queryAllName() {
        List<String> names = dao.queryAllName();
        return names;
    }

    /**
     * 查询用户页面所有的按钮
     *
     * @param userId
     * @return
     */

    @Override
    public Map<String, Integer> queryAllRightsBtn(int userId) {
        List<Map<String, Object>> resultAllBtn = sysMenuDao.queryAllRightsBtn(userId);
        //将item_code 的value放入集合进行返回,
        Map<String, Integer> blackMap = new HashMap<>();
        for (Map<String, Object> rstMap : resultAllBtn) {
            //把返回字段的值封装到另一个集合,方便返回
            blackMap.put((String) rstMap.get("item_code"), 1);
        }
        return blackMap;
    }

    @Override
    public int updateMeterial(String name, String sid, String phone,String accid) {
       int rst = dao.updateMeterial(name,sid,phone,accid);
        return rst;
    }

    @Override
    public List<String> querySidAndPhoneId(String sid, String pid) {
        return dao.querySidAndPhoneId(sid,pid);
    }

    /**
     * 递归查询下级菜单
     *
     * @param pmenuId
     * @return
     */
    //可以查询当前的用户信息，在形式参数价格 userid就行
    private List<MenuInfo> queryMenuInfo(Integer userId, Integer pmenuId) {
//        查询当前用户的菜单信息
        List<SysMenu> sysMenuList = sysMenuDao.queryMenu(userId, pmenuId);
        List<MenuInfo> rst = new ArrayList<>();
        sysMenuList.forEach(sysMenu -> {
            MenuInfo menuInfo = new MenuInfo();
            menuInfo.setTitle(sysMenu.getTitle());
            menuInfo.setHref(sysMenu.getHref());
            menuInfo.setIcon(sysMenu.getIcon());
            menuInfo.setTarget(sysMenu.getTarget());
            if (sysMenu.getChildNum() > 0) {
                menuInfo.setChild(queryMenuInfo(userId,sysMenu.getMenuId()));
            }
            rst.add(menuInfo);
        });
            return rst;
    }

}
